package thunderbirdgtd.global;

import java.io.*;
//Xerces 1 or 2 additional classes.
import org.apache.xml.serialize.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class XmlWriter {

	private static XMLSerializer serializer;
	private static ContentHandler hd;
	private static FileOutputStream fos;

	public static void writeHeader(String filename) {
		try {
			fos = new FileOutputStream(filename);
			OutputFormat of = new OutputFormat("XML","UTF-8",true);
			of.setIndent(1);
			of.setIndenting(true);
			of.setDoctype(null,"sportsml-specification/sportsml-control-file.dtd");
			serializer = new XMLSerializer(fos,of);
			hd = serializer.asContentHandler();
			hd.startDocument();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
	}
	
	public static void writeHeaderWithoutDoctype(String filename) {
		try {
			fos = new FileOutputStream(filename);
			OutputFormat of = new OutputFormat("XML","UTF-8",true);
			of.setIndent(1);
			of.setIndenting(true);
			serializer = new XMLSerializer(fos,of);
			hd = serializer.asContentHandler();
			hd.startDocument();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
	}

	public static void writeParentElement(String element) {

//		Processing instruction sample.
//		hd.processingInstruction("xml-stylesheet","type=\"text/xsl\" href=\"users.xsl\"");
//		USER attributes.
		AttributesImpl atts = new AttributesImpl();
//		USERS tag.
		try {
			hd.startElement("","",element, atts);
		} catch (SAXException e) {
			e.printStackTrace();
		}
	}

	public static void writeParentElementWithAtts(String element, String[] types, String[] content) {

//		Processing instruction sample.
//		hd.processingInstruction("xml-stylesheet","type=\"text/xsl\" href=\"users.xsl\"");
//		USER attributes.
		AttributesImpl atts = new AttributesImpl();
//		USERS tag.
		atts.clear();
		for (int i = 0; i < types.length; i++)
		{
			atts.addAttribute("","",types[i],"CDATA",content[i]);
		}
		try {
			hd.startElement("","",element, atts);
		} catch (SAXException e) {
			e.printStackTrace();
		}
	}

	public static void closeParentElement(String element) {
		try {
			hd.endElement("","",element);
		} catch (SAXException e) {
			e.printStackTrace();
		}
	}

	public static void closeFile() {
		try {
			hd.endDocument();
			fos.close();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void writeContent(String tag, String text) {
		AttributesImpl atts = new AttributesImpl();
		atts.clear();
		try {
			hd.startElement("","",tag,atts);
			hd.characters(text.toCharArray(), 0, text.toCharArray().length);
			hd.endElement("","",tag);
		} catch (SAXException e) {
			e.printStackTrace();
		}
	}
}
